﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Domain.Authentication;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class AdminMenu : UserControl, IAdminMenuView
    {
        private AdminMenuPresenter _presenter;

        #region IAdminMenuView Members

        public void SetLatestVisitedAdminUrls(IList<VisitedAdminUrl> urls)
        {
            rpLatestVisitedAdminUrls.DataSource = urls;
            rpLatestVisitedAdminUrls.DataBind();
        }

        public void SetLatestVisitedPublicUrls(IList<VisitedPublicUrl> urls)
        {
            rpLatestVisitedPublicUrls.DataSource = urls;
            rpLatestVisitedPublicUrls.DataBind();
        }

        public void SetCustomAdminPages(IList<Cms.Domain.CustomAdminPage> customAdminPages)
        {
            rpCustomAdminPages.DataSource = customAdminPages;
            rpCustomAdminPages.DataBind();
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/AdminMenu.xml"; }
        }

        public string Localization
        {
            get { return (Page as AdminBasePage).Localization; }
        }
        
        public virtual int AdministratorID
        {
            get
            {
                AuthenticatedUser user = Conversion.ToAuthenticatedUser(Page.User.Identity.Name);
                return user.AdministratorID;
            }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new AdminMenuPresenter(this);
            if (Page.IsPostBack)
                return;
            _presenter.Initialize();
            Administrator admin = _presenter.GetAdministrator();
            hlSites.Visible = admin.HasPermission.Sites;
            hlExplorer.Visible = admin.HasPermission.Explorer;
            hlUserGroups.Visible = admin.HasPermission.UserGroups;
            hlUsers.Visible = admin.HasPermission.Users;
            hlAdministratorGroups.Visible = admin.HasPermission.AdministratorGroups;
            hlAdministrators.Visible = admin.HasPermission.Administrators;
            hlCustomAdminPages.Visible = admin.HasPermission.CustomAdminPages;
            hlUpdate.Visible = admin.HasPermission.Update;
            hlCustomizeEditor.Visible = admin.HasPermission.CustomizeEditor;
            divSystemDivider1.Visible = (
                                            hlUpdate.Visible ||
                                            hlCustomizeEditor.Visible
                                        )
                                        && 
                                        (
                                            hlSites.Visible ||
                                            hlAdministrators.Visible ||
                                            hlAdministratorGroups.Visible ||
                                            hlUserGroups.Visible ||
                                            hlUsers.Visible ||
                                            hlDashboardGadgets.Visible ||
                                            hlCustomAdminPages.Visible
                                        );
            hlDashboardGadgets.Visible = admin.HasPermission.DashboardGadgets;
            hlSystemGroup.Visible = hlSites.Visible ||
                                    hlAdministrators.Visible ||
                                    hlAdministratorGroups.Visible ||
                                    hlUserGroups.Visible ||
                                    hlUsers.Visible ||
                                    hlDashboardGadgets.Visible ||
                                    hlCustomAdminPages.Visible ||
                                    hlUpdate.Visible;
            // Url lists should only be visible when they contain links
            hlPublicTraceGroup.Visible = rpLatestVisitedPublicUrls.Items.Count > 0;
            hlAdminTraceGroup.Visible = rpLatestVisitedAdminUrls.Items.Count > 0;
            hlCustomAdminPagesGroup.Visible = rpCustomAdminPages.Items.Count > 0;
        }
        
        protected void rpLatestVisitedAdminUrls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }
            var visit = (VisitedAdminUrl) e.Item.DataItem;
            var litUrl = (Literal) e.Item.FindControl("litUrl");
            litUrl.Text = "<a href=\"" + visit.Url + "\"><img src=\"" + visit.ImageUrl + "\" /><span>" +
                          visit.Header + " - " + TrimUrl(visit.Url) + "</span></a>";
        }

        protected void rpLatestVisitedPublicUrls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }
            var visit = (VisitedPublicUrl) e.Item.DataItem;
            var litUrl = (Literal) e.Item.FindControl("litUrl");
            string url = Url.VirtualPath + visit.Page.GetVirtualPath();
            if (!string.IsNullOrEmpty(visit.QueryString))
            {
                url += "?" + visit.QueryString;
            }
            litUrl.Text = "<a href=\"" + url + "\"><img src=\"";
            litUrl.Text += visit.Page is ApplicationPage ? "Admin/Img/icons/page_code.png" : "Admin/Img/icons/page.png";
            litUrl.Text += "\" /><span>";
            litUrl.Text += visit.Page.Name + " - " + TrimUrl(url);
            litUrl.Text += "</span></a>";
        }

        private string TrimUrl(string url)
        {
            if (url.Length > 70)
            {
                url = url.Substring(0, 70) + "&#133;";
            }
            return url;
        }
        
        protected void rpCustomAdminPages_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }
            var customAdminPage = (Cms.Domain.CustomAdminPage)e.Item.DataItem;
            var litUrl = (Literal)e.Item.FindControl("litUrl");
            litUrl.Text = "<a href=\"__System/CustomAdminPages/" + 
                            customAdminPage.Filename + "\"><img src=\"Admin/Img/icons/page_white_visualstudio.png\" /><span>" + 
                            customAdminPage.Name + "</span></a>";
        }

    }
}